/*
 * Created by SharpDevelop.
 * User: MyWife
 * Date: 1/1/2003
 * Time: 1:36 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Collections;
using popLib;

namespace MailCheckerGadget
{
	/// <summary>
	/// Description of MailCheckerPanel.
	/// </summary>
	//public partial class MailCheckerPanel : UserControl
	public partial class MailCheckerPanel : SidebarControl
	{
		popLib.ExtensionClass.Imap server;
		Thread main;
		int unreadMail = 0;
		string host, id, pw;
		bool connected = false, ssl = false;
		
		
		public MailCheckerPanel(SidebarGadget sidebarGadget):base(sidebarGadget)
		{
			InitializeComponent();
			
			updateControlBackground(pnl_option);
			updateControlBackground(pnl_refresh);
			updateControlBackground(lbl_counter);
			
		}	
		
		private void updateControlBackground(Control control){
			control.BackgroundImage = ImageLib.cropImage( this.BackgroundImage, control.Location.X, control.Location.Y, control.Width, control.Height );
			control.MouseHover += delegate { this.Parent.BringToFront(); };
		}
		
		public override void StartComponent(){
			Hashtable options = sidebarGadget.Options;
			
			host = options[Start.optionsType.Host+""].ToString();
			id = options[Start.optionsType.Username+""].ToString();
			pw = options[Start.optionsType.Password+""].ToString();
			ssl = ( options[Start.optionsType.SSL+""].ToString() == "1" ) ? true : false;
			timer1.Interval = Convert.ToInt16( options[Start.optionsType.RequestDelay+""].ToString() ) * 1000;
			toolTip1.SetToolTip( lbl_counter, host.Split(':')[0] );
			
			connected = false;
			
			if( StringLib.isValid( host ) && StringLib.isValid( id ) && StringLib.isValid( pw ) ){
				Timer1Tick( null, new EventArgs() );
				timer1.Start();
			}else{
				updateStatus( "Error!", "Mail server information is not valid!" );
			}
			
		}
		
		void checkMail(){
			try{
				if( !connected ){
					ReturnSet rs = NetLib.createImapServer( host, id, pw, ssl );
					if( rs.Success ){
						
						server = (popLib.ExtensionClass.Imap)rs.Result;
						timer1.Start();
						connected = true;
					}else{
						updateStatus( "Error!", "Cannot connect to server!" );
					}
				}
				
				if( connected ){
					string emailCounter = server.UnreadMail > 0 ? (server.TotalMail + "("+server.UnreadMail+")") : server.TotalMail+"";
					updateStatus( emailCounter, emailCounter );
					bool alwaysNotifyNewMail = (sidebarGadget.Options[Start.optionsType.AlwaysNotifyNewMail+""]+"" == "1") ? true : false;
					
					if( server.UnreadMail > unreadMail || (server.UnreadMail > 0 && alwaysNotifyNewMail) ){
						unreadMail = server.UnreadMail;
						sidebarGadget.sidebarContainer.ShowBalloonTip( "You have new mail.", server.UnreadMail +"/"+ server.TotalMail, ToolTipIcon.Info );							
					}
				}
				GC.Collect();
			}catch(Exception ex){ connected = false; ex.ToString(); }
		}
		
		void Timer1Tick(object sender, EventArgs e)
		{
			updateStatus( "Refreshing...", "Reconnecting to server to get new email" );
			if( main != null && main.IsAlive ) main.Abort();
			main = new Thread( new ThreadStart( checkMail ) );
			main.IsBackground = true;
			main.Start();
		}
		
		public delegate void updateStatusDelegate(string status, string tooltip);
		public void updateStatus(string status, string tooltip){
			TextBox a = new TextBox();
			if( lbl_counter.InvokeRequired ){
				lbl_counter.BeginInvoke( new updateStatusDelegate(updateStatus), status );
			}else{
				lbl_counter.Text = status;
				toolTip1.SetToolTip(lbl_counter,tooltip);
			}
		}
		
		void ViewOption(object sender, EventArgs e)
		{
			sidebarGadget.ViewOptions();
		}		
		
		void Refresh(object sender, EventArgs e)
		{
			Timer1Tick( null, new EventArgs() );
		}
	}
}
